//
//  MutilTextSettingCellData.m
//  18Birdies
//
//  Created by poleness on 16/5/17.
//  Copyright © 2016年 Lc. All rights reserved.
//

#import "LcFoundation.h"
#import "MutilTextSettingCell.h"
#import "MutilTextSettingCellData.h"

@implementation MutilTextSettingCellData

+ (instancetype)cellDataWithTitle:(NSString*)title
                          content:(NSString*)content
              disclosureIndicator:(BOOL)disclosureIndicator
                andOnClickHandler:(OnCellClickedHanlder)onClickedHandler {
  LC_CHECK_TRUE(title.length > 0 || content.length > 0);
  title = title.length > 0 ? title : @"";
  content = content.length > 0 ? content : @"";
  NSAttributedString* titleStr = nil;
  NSAttributedString* contentStr = nil;
  if (title.length > 0) {
    titleStr = [NSAttributedString attributedStringWithString:title lcFont:[LcFont gr17]];
  }
  if (content.length > 0) {
    contentStr = [NSAttributedString attributedStringWithString:content lcFont:[LcFont dm17]];
  }

  return [self cellDataWithAttrTitle:titleStr
                         attrContent:contentStr
                 disclosureIndicator:disclosureIndicator
                   andOnClickHandler:onClickedHandler];
}

+ (instancetype)cellDataWithAttrTitle:(NSAttributedString*)attrTitle
                          attrContent:(NSAttributedString*)attrContent
                  disclosureIndicator:(BOOL)disclosureIndicator
                    andOnClickHandler:(OnCellClickedHanlder)onClickedHandler {
  MutilTextSettingCellData* cellData = [[MutilTextSettingCellData alloc] init];
  cellData->_title = attrTitle;
  cellData->_content = attrContent;
  cellData.clickable = disclosureIndicator;
  cellData.onCellClickedHandler = onClickedHandler;
  cellData.titleLabelPoint = CGPointMake(15, 12);
  cellData.titleFont = LcFont.gr17;
  return cellData;
}

+ (instancetype)cellDataWithTitle:(NSString*)title
                          content:(NSString*)content
              disclosureIndicator:(BOOL)disclosureIndicator
              contentClickHandler:(OnClickedHandler)contentClickHandler
                andOnClickHandler:(OnCellClickedHanlder)onClickedHandler {
  LC_CHECK_TRUE(title.length > 0 || content.length > 0);
  title = title.length > 0 ? title : @"";
  content = content.length > 0 ? content : @"";
  NSAttributedString* titleStr = nil;
  NSAttributedString* contentStr = nil;
  if (title.length > 0) {
    titleStr = [NSAttributedString attributedStringWithString:title lcFont:[LcFont gr17]];
  }
  if (content.length > 0) {
    contentStr = [NSAttributedString attributedStringWithString:content lcFont:[LcFont dm17]];
  }

  MutilTextSettingCellData* cellData = [[MutilTextSettingCellData alloc] init];
  cellData->_title = titleStr;
  cellData->_content = contentStr;
  cellData->_contentClickHandler = contentClickHandler;
  cellData.clickable = disclosureIndicator;
  cellData.onCellClickedHandler = onClickedHandler;
  cellData.titleLabelPoint = CGPointMake(15, 12);
  cellData.titleFont = LcFont.gr17;
  return cellData;
}

- (Class)cellClazz {
  return MutilTextSettingCell.class;
}

- (NSString*)nibName {
  return @"MutilTextSettingCell";
}

- (void)refreshCell:(MutilTextSettingCell*)cell {
  [super refreshCell:cell];
  cell.titleLabel.attributedText = self.title;
  cell.contentLabel.attributedText = self.content;
  cell.titleLabelLeftMargin.constant = self.titleLabelPoint.x;
  cell.titleLabelTopMargin.constant = self.titleLabelPoint.y;

  if (self.clickable) {
    cell.detailLabelRightMargin.constant = 30;
    cell.disclosureIndicator.hidden = NO;
  } else {
    cell.detailLabelRightMargin.constant = 15;
    cell.disclosureIndicator.hidden = YES;
  }

  if (self.titleWidthMax == 0 && self.title.string.length > 0) {
    CGSize titleSize =
        [self.title.string sizeOfFont:self.titleFont.font andWidth:LcUIUtility.screenWidth];
    self.titleWidthMax = titleSize.width + 1;
  }
  cell.titleLabel.preferredMaxLayoutWidth = self.titleWidthMax;
  cell.contentLabel.preferredMaxLayoutWidth =
      LcUIUtility.screenWidth - self.titleWidthMax - 15 - 5 - cell.detailLabelRightMargin.constant;

  if (self.contentClickHandler) {
    [cell.contentButton setLcFont:self.nomalStateFont];
    [cell.contentButton setTitle:self.content.string forState:UIControlStateNormal];
    [cell.contentButton setTitle:self.content.string forState:UIControlStateHighlighted];
    [cell.contentButton setTitle:self.content.string forState:UIControlStateSelected];
    [cell.contentButton setTitleColor:self.selectStateColor forState:UIControlStateHighlighted];
    [cell.contentButton setTitleColor:self.selectStateColor forState:UIControlStateSelected];
    cell.contentButton.hidden = NO;
    cell.contentLabel.hidden = YES;
    [cell.contentButton removeTarget:nil
                               action:nil
                     forControlEvents:UIControlEventTouchUpInside];
    [cell.contentButton addTarget:self
                            action:@selector(buttonClicked:)
                  forControlEvents:UIControlEventTouchUpInside];
  } else {
    cell.contentButton.hidden = YES;
    [cell.contentButton removeTarget:nil
                              action:nil
                    forControlEvents:UIControlEventTouchUpInside];
  }
}

- (void)setTextContent:(NSString*)contextText {
  if (contextText.length > 0) {
    self.content = [NSAttributedString attributedStringWithString:contextText lcFont:[LcFont dm17]];
  } else {
    self.content = nil;
  }
  self.cellHeight = 0;
}

- (void)buttonClicked:(UIButton*)sender {
  if (self.contentClickHandler) {
    self.contentClickHandler(sender);
  }
}

@end
